package com.sto.stoapp.activity.aviation;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.sto.stoapp.activity.R;
import com.sto.stoapp.adapter.RouteListAdapter;
import com.sto.stoapp.db.manager.RouteManager;
import com.sto.stoapp.entity.RouteEntity;
import com.sto.stoapp.thread.BaseThread;
import com.sto.stoapp.widget.BaseActivity;

import java.util.List;

/**
 * Created by wj on 2016-12-19.
 */

public class StoAviSelectRouteActivity extends BaseActivity{

    private ListView updateList;
    private TextView title;

    private RouteListAdapter courierAdapter;
    private List<RouteEntity> courierDatas;

    private String routeCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_avi_select_route);

        routeCode=getIntent().getStringExtra("routeCode");

        initWidget();

        updateList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                RouteEntity entity=courierDatas.get(position);
                Intent intent=new Intent();
                intent.putExtra("route",entity);
                setResult(0,intent);
                finish();
            }
        });
    }

    private void initWidget() {
        title = (TextView) findViewById(R.id.title);
        updateList = (ListView) findViewById(R.id.update_listview);
        title.setText(getString(R.string.select_route_list));
        BufferThread bufferThread = new BufferThread(this);
        bufferThread.start();
    }

    class BufferThread extends BaseThread {

        private Context context;

        public BufferThread(Context context) {
            super(context);
            this.context = context;
        }

        @Override
        public void run() {
            RouteManager routeManager = RouteManager.getInstance(context);
            if(TextUtils.isEmpty(routeCode)){
                courierDatas = routeManager.queryAll();
            }else {
                courierDatas = routeManager.queryFuzzyAll(routeCode);
            }

            final int count = courierDatas == null ? 0 : courierDatas.size();

            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    courierAdapter = new RouteListAdapter(context,
                            courierDatas);
                    updateList.setAdapter(courierAdapter);
                    if (count > 0) {
                        updateList.requestFocus();
                    }
                }
            });
        }
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        if(courierDatas != null){
            courierDatas.clear();
            courierDatas = null;
        }
        super.onDestroy();
    }
}
